<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
      list-style: none;
    }

    .carousel {
      margin: 100px auto;
      width: 800px;
      height: 350px;
      border: 10px solid orange;
    }

    .cal-list {
      position: relative;
      left: 0;
      top: 0;
      width: 800px;
      height: 350px;
      overflow: hidden;
    }

    .viewpage {
      position: absolute;
      left: 0;
      top: 0;
      width: 700%;
      height: 350px;
    }

    .item {
      float: left;
    }

    img {
      width: 800px;
      height: 350px;
    }
  </style>

</head>

<body>
  <div class="carousel">
    <div class="cal-list">
      <ul class="viewpage">
        <li class="item"><img src="../img/01.png" alt="" /></li>
        <li class="item"><img src="../img/02.png" alt="" /></li>
        <li class="item"><img src="../img/03.png" alt="" /></li>
        <li class="item"><img src="../img/04.png" alt="" /></li>
        <li class="item"><img src="../img/05.png" alt="" /></li>
        <li class="item"><img src="../img/06.png" alt="" /></li>
        <li class="item"><img src="../img/01.png" alt="" /></li>
      </ul>
    </div>
  </div>

  <script src="../js/animationutils.js"></script>
  <script>
    var viewpage = document.getElementsByClassName("viewpage")[0]
    var calList = document.getElementsByClassName("cal-list")[0]
    var items = document.getElementsByClassName("item");

    var index = 0;

    // 1 自动轮播
    var inteId
    function autoPlay () {
      inteId = setInterval(function () {
        index++;
        if (index >= items.length) {
          viewpage.style.left = "0px"
          index = 1;
        }

        var target = -800 * index;
        console.log(target, index, items.length);
        animationMove(viewpage, target)
      }, 2000)
    }

    autoPlay()


    //2 悬停暂停, 离开继续
    calList.onmouseover = function (e) {
      clearInterval(inteId)
    }
    calList.onmouseleave = function (e) {
      autoPlay()
    }

  </script>

</body>

</html>